
cc.Class({
    extends: cc.Component,

    properties: {
        buttons: [cc.Node],

        copyNode: cc.Node,

        content: cc.Node,

        _curr_time_s_n: 0,
        _step_time_s_n: 0.01,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this._moveOver = true;
        this._moveDist = 130;
        this._curMoveDist = 0;
        this._moveSpeed = 150;
    },

    start() {
        let startComp = this.node.getComponent("start");

        this.buttons[0].on('click', startComp.btn_打乱, startComp);
        this.buttons[1].on('click', startComp.btn_随机抽奖, startComp);
        this.buttons[2].on('click', startComp.btn_必中抽奖, startComp);

        // this.content.getComponent(cc.Layout).enable = true;

        this.scheduleOnce(this.addCopyNode, 1);
    },

    addCopyNode() {

        for (let i = 0; i < this.content.childrenCount; i++) {
            if (i < 1) {
                this.content.children[i].zIndex = i;
            } else {
                this.content.children[i].zIndex = i + 1;
            }

        } //
        // 
        let xPos = this.getElementPos(1);
        let dPos = this.content.convertToWorldSpaceAR(cc.v2(xPos, 0));

        this.orgPos = this.copyNode.position;
        this.destPos = this.copyNode.parent.convertToNodeSpaceAR(dPos);
        this.otherFlyDist = this.destPos.sub(this.copyNode.position).mag();
        let dx =this.destPos.x - this.orgPos.x;
        let dy = this.destPos.y - this.orgPos.y;
        this.dxRate = dx / this.otherFlyDist;
        this.dyRate = dy / this.otherFlyDist;

        // this.content.addChild(this.copyNode, 1);
        this._moveOver = false;
    },

    update(dt) {
        if (this._moveOver) {
            return;
        }
        if (this._curMoveDist < this._moveDist) {
            this._curr_time_s_n += dt;
            if (this._curr_time_s_n >= this._step_time_s_n) {
                this._curr_time_s_n -= this._step_time_s_n;
                this._curMoveDist += this._step_time_s_n * this._moveSpeed;
                let dist_scale_n = Math.round(this._curMoveDist / Math.abs(this._moveDist) * 100) / 100;
                let moveDist = this.otherFlyDist * dist_scale_n;
                this.refreshCopyPos(moveDist);
                this.moveAllExceptFirst(this._step_time_s_n * this._moveSpeed);
            }
        } else {
            this._moveOver = true;
            this.resetAllChildrenPos();
        }

    },
    
    // 往下飞 确定
    refreshCopyPos(moveDist) {
        // DX DY  sqrt(DX * DX + DY * DY)
        let pos2 = this.destPos;
        let pos1 = this.orgPos;
        let lineRate = 0;
        let yDis;
        let angle;
        if (pos2.x == pos1.x) {
            yDis = pos2.y - pos1.y;
        } else {
            lineRate = (pos1.y - pos2.y) / (pos1.x - pos2.x); // 负数 x 增加  正数 x 减小
            angle = Math.atan(lineRate) * 180 / Math.PI;
        }
        if (lineRate === 0) {
            this.copyNode.y = pos1.y + ((yDis < 0) ? - moveDist : moveDist);
        } else {
            // let xOffset = Math.cos(angle) * moveDist;
            // let yOffSet = Math.sin(angle) * moveDist;
            // this.copyNode.position = cc.v2(pos1.x + xOffset, pos1.y + yOffSet);

            let xOffset = this.dxRate * moveDist;
            let yOffSet = this.dyRate * moveDist;
            this.copyNode.position = cc.v2(pos1.x + xOffset, pos1.y + yOffSet);
        }
    },


    // anchorX 0 child x 0.5
    getElementPos(index) {
        return this.content.children[0].width * 0.5 * (1 + 2 * index);
    },

    resetAllChildrenPos() {
        this.copyNode.parent = null;
        this.content.addChild(this.copyNode, 1);
        for (let i = 1; i < this.content.childrenCount; i++) {
            this.content.children[i].x = this.getElementPos(this.content.children[i].zIndex);
            this.content.children[i].y = 0;
        }
    },

    moveAllExceptFirst(moveDist) {

        for (let i = 1; i < this.content.childrenCount; i++) {
            this.content.children[i].x += moveDist;
        }
    },
});
